<template>
  <div class="login-body">
    <vue-particles
      class="login-bg"
      color="#fff"
      :particleOpacity="0.7"
      :particlesNumber="100"
      shapeType="circle"
      :particleSize="4"
      linesColor="#8DD1FE"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
    <div class="login-data">
      <div class="login-text">
        <h2>个人博客管理系统</h2>
      </div>
      <div class="formdata">
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              clearable
              prefix-icon="el-icon-user"
              placeholder="请输入账号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              clearable
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="tool">
        <div>
          <el-checkbox
            style="color: #fff"
            v-model="checked"
            @change="remenberPass"
            >记住密码</el-checkbox
          >
        </div>
        <div>
          <span @click="forgetPass">忘记密码？</span>
        </div>
      </div>
      <div class="butt">
        <el-button type="primary" @click.native.prevent="login">登录</el-button>
        <el-button @click="register">注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      checked: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { max: 10, message: '不能大于10个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { max: 10, message: '不能大于10个字符', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    if (localStorage.getItem('news')) {
      this.form = JSON.parse(localStorage.getItem('news'))
      this.checked = true
    }
  },
  methods: {
    // 登录
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$http.post(`/login/admin`, this.form).then((res) => {
            if (res.token) {
              localStorage.setItem('token', res.token)
              this.$router.push('home/dashboard')
              this.$message({
                type: 'success',
                message: '登录成功!',
                showClose: true,
                duration: 1500,
              })
            } else {
              this.$message({
                type: 'error',
                message: '账号或者密码不正确,请重新输入!',
                showClose: true,
                duration: 1500,
              })
            }
          })
        } else {
          return false
        }
      })
    },
    // 记住密码
    remenberPass(data) {
      this.checked = data
      if (this.checked) {
        localStorage.setItem('news', JSON.stringify(this.form))
      } else {
        localStorage.removeItem('news')
      }
    },
    // 忘记密码
    forgetPass() {
      this.$message({
        type: 'info',
        message: '功能尚未开发额😥',
        showClose: true,
      })
    },
    // 注册
    register() {
      this.$message({
        type: 'info',
        message: '功能尚未开发额😥',
        showClose: true,
      })
    },
  },
}
</script>

<style lang="stylus" scoped>
// 引入主题配色
@import "styles/theme.styl";
.login-body
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  .login-bg
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background: $blackdark;
  .login-data
    width: 400px;
    padding: 30px;
    border-radius: 5px;
    background-color: rgba(51, 51, 51, 0.5);
    box-shadow: 0 0 10px $white;
    z-index: 1
    .login-text
      text-align: center;
      margin-bottom: 20px;
      font-size: 30px;
      font-weight: bolder;
      color: #fff;
      text-shadow: 3px 3px 4px #000000;
    .tool
      display: flex;
      justify-content: space-between;
      color: #fff;
      font-size: 14px;
    .butt
      margin-top: 10px;
      text-align: center;
</style>
